package com.wva.db.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.wva.common.context.UserContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;

import java.time.LocalDateTime;

/**
 * @Author: hong
 * @Date: 2025/2/11 14:30:36
 */

@Slf4j
public class AutoFillHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("自动填充");
        //记录创建时间
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        //记录创建用户
        if (UserContext.isLogin()) {
            this.strictInsertFill(metaObject, "createBy", Long.class, UserContext.getUserId());
        }

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("自动更新");
        //记录更新时间
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        //记录更新用户
        if (UserContext.isLogin()) {
            this.strictUpdateFill(metaObject, "updateBy", Long.class, UserContext.getUserId());
        }
    }
}
